package api

import (
	"blog/library"
	"blog/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type BlogController struct {
	base
}

func (self *BlogController) NewBlog() {
	beego.Error(self.Ctx.Request.PostForm)
	self.ServeJSON()
}

func (self *BlogController) GetBlogList() {
	//page, _ := self.GetInt64("page", 1)
	//limit, _ := self.GetInt64("limit", 1)
	//var queryBuilder models2.QueryBuilder
	//queryBuilder.Limit = limit
	//queryBuilder.Page = page
	//queryBuilder.Order = []string{"id"}
	//categoryList, total, _ := blog.GetBlogList(queryBuilder)
	api := library.Api{}
	api.Data = []int{}
	api.Total = 1
	self.Data["json"] = api
	self.ServeJSON()
}

func (self *BlogController) GetCategoryList() {
	page, _ := self.GetInt("page", 1)
	limit, _ := self.GetInt("limit", 1)
	name := self.GetString("name", "")
	var condition = orm.NewCondition()
	if name != "" {
		condition = condition.And("name", name)
	}
	categoryList, total, _ := models.GetList(models.CategoryTable(), page, limit, condition, "id", "-name")
	api := library.Api{}
	api.Data = categoryList
	api.Total = total
	self.Data["json"] = api
	self.ServeJSON()
}

func (self *BlogController) getList(k int, total int) []map[string]string {
	var blogList []map[string]string
	for ; k < total; k++ {
		blog := map[string]string{}
		blog["id"] = strconv.Itoa(k)
		blog["name"] = strconv.Itoa(k)

		blogList = append(blogList, blog)
	}
	return blogList
}

func (self *BlogController) NewBlogCategory() {
}

func (self *BlogController) BlogView() {
	//p, _ := self.GetInt("id", 0)
	//self.success(p)
}
